package com.uam.web.common.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.uam.common.constant.Constants;
import com.uam.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;

public class ResourceUtils {

    private static Logger log = LoggerFactory.getLogger(ResourceUtils.class);

    /**
     * 从resource目录下读取文件，如 syslayer/file.geojson
     */
    public static String getResourceFile(String resourcePath) {
        if (StringUtils.isEmpty(resourcePath)) {
            return "";
        }
        StringBuffer sb = new StringBuffer();
        try (InputStream in = ResourceUtils.class.getClassLoader().getResourceAsStream(resourcePath)) {
            final int maxSize = 1024;
            byte[] buff = new byte[maxSize];
            int len = 0;
            while ((len = in.read(buff, 0, maxSize)) > 0) {
                sb.append(new String(buff, 0, len, Constants.UTF8));
            }
        } catch (Exception e) {
            log.error("读取文件出现异常", e);
        }
        return sb.toString();
    }

    /**
     * 从绝对路径读取文件，如 /home/data/syslayer/file.geojson
     */
    public static String getFileContent(String absolutePath) {
        if (StringUtils.isEmpty(absolutePath)) {
            return "";
        }
        StringBuffer sb = new StringBuffer();
        try (InputStream in = new FileInputStream(absolutePath)) {
            final int maxSize = 1024;
            byte[] buff = new byte[maxSize];
            int len = 0;
            while ((len = in.read(buff, 0, maxSize)) > 0) {
                sb.append(new String(buff, 0, len, Constants.UTF8));
            }
        } catch (Exception e) {
            log.error("读取文件出现异常", e);
        }
        return sb.toString();
    }

    /**
     * 更新文件内容
     */
    public static boolean updateFileContent(String absolutePath, String content) {
        String fileContent = getFileContent(absolutePath);
        if (StringUtils.isEmpty(fileContent)) {
            log.error("读取文件有误，absolutePath：{}", absolutePath);
            return false;
        }
        JSONObject contentJs = JSONObject.parseObject(content);
        JSONObject js = JSONObject.parseObject(fileContent);
        JSONArray ja = js.getJSONArray("features");
        for (int i = 0; i < ja.size(); i++) {
            JSONObject jso = ja.getJSONObject(i);
            JSONObject properties = jso.getJSONObject("properties");
            if (properties.getString("id").equals(contentJs.getString("id"))) {
                jso.put("properties", contentJs);
                break;
            }
        }
        try (FileOutputStream in = new FileOutputStream(absolutePath)) {
            in.write(JSON.toJSONString(js, SerializerFeature.DisableCircularReferenceDetect).getBytes());
            return true;
        } catch (Exception e) {
            log.error("更新文件出现异常", e);
            return false;
        }
    }
}
